package com.firesrc.web;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.util.StringUtil;

import com.firesrc.bean.MailHost;
import com.firesrc.service.MailHostService;
import com.firesrc.service.impl.MailHostServiceImpl;

/**
 * Servlet implementation class MailHostServlet
 */
public class MailHostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MailHostServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		MailHostService service = new MailHostServiceImpl();
		String type = request.getParameter("type");
		if ("add".equals(type)) {
			MailHost mailHost = new MailHost();
			mailHost.setId(UUID.randomUUID().toString());
			mailHost.setMail(request.getParameter("mail"));
			mailHost.setHostName(request.getParameter("hostName"));
			mailHost.setUserName(request.getParameter("userName"));
			mailHost.setPassword(request.getParameter("password"));
			mailHost.setKind(request.getParameter("kind"));
			mailHost.setState(request.getParameter("state"));
			mailHost.setSendOnceMaxCount(Integer.parseInt(request.getParameter("sendOnceMaxCount")));
			mailHost.setSendDayMaxCount(Integer.parseInt(request.getParameter("sendDayMaxCount")));
			mailHost.setSsl(Boolean.valueOf(request.getParameter("isSsl")));
			service.add(mailHost);
		} else if ("del".equals(type)) {
			service.delete(request.getParameter("id"));
		} else if ("enable".equals(type)) {
			service.enable(request.getParameter("id"));
		} else if ("disable".equals(type)) {
			service.disable(request.getParameter("id"));
		} else if ("disableAll".equals(type)) {
			service.disableAll();
		} else if ("enableAll".equals(type)) {
			service.enableAll();
		} else if ("reset".equals(type)) {
			service.resetSeq(Integer.valueOf(request.getParameter("count")));
		}

		String kind = request.getParameter("kind");
		List<MailHost> mailHostList = new LinkedList<MailHost>();
		if (StringUtil.isEmpty(kind)) {
			mailHostList = service.getAllList();
		} else {
			mailHostList = service.getListByType(kind);
		}
		request.setAttribute("list", mailHostList);
		request.setAttribute("map", service.getKindCountMap(null));

		request.getRequestDispatcher("/maillist.jsp").forward(request, response);
	}
}
